<?php


namespace app\api\controller;


use app\admin\model\AdminLog;

class Notify
{
    public function start_pay_call(){
        $testxml  = file_get_contents("php://input");
        $jsonxml = json_encode(simplexml_load_string($testxml, 'SimpleXMLElement', LIBXML_NOCDATA));
        $result = json_decode($jsonxml, true);//转成数组，
        AdminLog::create([
            'admin_id'    => 0,
            'title'       => 'start_pay_call',
            'path'        => 'start_pay_call',
            'ip'          => '',
            'post'        => json_encode($result),
            'create_time' => date('Y-m-d H:i:s')
        ]);

        if($result){
            //如果成功返回了
            $out_trade_no = $result['out_trade_no'];
            if($result['return_code'] == 'SUCCESS'){
                if($result['result_code'] == 'SUCCESS'){
                    $order=(new \app\admin\model\Order())->where('start_order_sn',$out_trade_no)->findOrEmpty()->toArray();
                    if(empty($order)){
                        exit('<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>');
                    }
                    if($order['init_pay_status']==0){
                        (new \app\admin\model\Order())->where('start_order_sn',$out_trade_no)->update(['init_pay_status'=>1,'init_pay_time'=>date('Y-m-d H:i:s',time()-72*3600)]);
                    }
                    exit('<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>');
                }else{
                    $order=(new \app\admin\model\Order())->where('start_order_sn',$out_trade_no)->findOrEmpty()->toArray();
                    if(empty($order)){
                        exit('<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>');
                    }
                    if($order['init_pay_status']==0){
                        (new \app\admin\model\Order())->where('start_order_sn',$out_trade_no)->update(['init_pay_status'=>3,'init_pay_time'=>date('Y-m-d H:i:s')]);
                    }
                    exit('<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>');
                }
            }
        }
    }

    public function refund_pay_call(){
        $testxml  = file_get_contents("php://input");
        $jsonxml = json_encode(simplexml_load_string($testxml, 'SimpleXMLElement', LIBXML_NOCDATA));
        $result = json_decode($jsonxml, true);//转成数组，
        AdminLog::create([
            'admin_id'    => 0,
            'title'       => 'refund_pay_call',
            'path'        => 'refund_pay_call',
            'ip'          => '',
            'post'        => json_encode($result),
            'create_time' => date('Y-m-d H:i:s')
        ]);
        if($result){
            if($result['return_code']=='SUCCESS'){
                $req_info=$this->decryptData($result['req_info']);
                if($req_info){
                    $order=(new \app\admin\model\Order())->where('refund_order_sn',$req_info['out_refund_no'])->findOrEmpty()->toArray();
                    if($order){
                        if($req_info['refund_status']=='SUCCESS'){
                            (new \app\admin\model\Order())->where('refund_order_sn',$req_info['out_refund_no'])->save(['sign_type'=>3,'refund_pay_time'=>date('Y-m-d H:i:s'),'refund_status'=>1]);
                        }else{
                            (new \app\admin\model\Order())->where('refund_order_sn',$req_info['out_refund_no'])->save(['sign_type'=>2,'refund_pay_time'=>date('Y-m-d H:i:s'),'refund_status'=>3]);
                        }
                        exit('<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>');
                    }
                }
            }
        }
        exit('<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>');
    }

    public function end_pay_call(){
        $testxml  = file_get_contents("php://input");
        $jsonxml = json_encode(simplexml_load_string($testxml, 'SimpleXMLElement', LIBXML_NOCDATA));
        $result = json_decode($jsonxml, true);//转成数组，
        AdminLog::create([
            'admin_id'    => 0,
            'title'       => 'end_pay_call',
            'path'        => 'end_pay_call',
            'ip'          => '',
            'post'        => json_encode($result),
            'create_time' => date('Y-m-d H:i:s')
        ]);
        if($result){
            //如果成功返回了
            $out_trade_no = $result['out_trade_no'];
            if($result['return_code'] == 'SUCCESS'){
                if($result['result_code'] == 'SUCCESS'){
                    $order=(new \app\admin\model\Order())->where('end_order_sn',$out_trade_no)->findOrEmpty()->toArray();
                    if(empty($order)){
                        exit('<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>');
                    }
                    if($order['again_pay_status']==2){
                        (new \app\admin\model\Order())->where('end_order_sn',$out_trade_no)->update(['again_pay_status'=>1,'end_pay_time'=>date('Y-m-d H:i:s'),'sign_type'=>3]);
                    }
                    exit('<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>');
                }else{
                    $order=(new \app\admin\model\Order())->where('end_order_sn',$out_trade_no)->findOrEmpty()->toArray();
                    if(empty($order)){
                        exit('<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>');
                    }
                    if($order['again_pay_status']==0){
                        (new \app\admin\model\Order())->where('end_order_sn',$out_trade_no)->update(['again_pay_status'=>3,'end_pay_time'=>date('Y-m-d H:i:s')]);
                    }
                    exit('<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>');
                }
            }
        }
    }

    public function decryptData($reqInfo) {
        $reqData = openssl_decrypt(base64_decode($reqInfo), 'AES-256-ECB', md5('e1549f7b56377c935b1c29794a8da341'),OPENSSL_RAW_DATA, '');
        if (!$reqData) return false;
        return $this->xml2arr($reqData);
    }
    public function xml2arr($xml){
        $xml = new \SimpleXMLElement($xml);
        if(!is_object($xml)){
            $this->errmsg = "xml数据接收错误";
            return false;
        }
        $arr = array();
        foreach ($xml as $key => $value) {
            $arr[strtolower($key)] = strval($value);
        }
        return $arr;
    }
}